<?php
	
	/*
	 	Handler for the login module
	
	*/
	
	class LoginHandler
	{
		private $loggedIn = "loggedIn";
		
		private $m_db = NULL;
		
		public function __construct(Database $db)
		{
			$this->m_db = $db;
		}
		
		/* Returns a value indicating if a user is logged in or not */
		public function IsLoggedIn()
		{
			if (!isset($_SESSION[$this->loggedIn]))
				return false;
			
			return $_SESSION[$this->loggedIn];
		}
		
		/* Attempts to log in a user with passed in parameters, and return the result */
		public function DoLogin($username, $password)
		{
			// Suppose the log-in was successfull
			$_SESSION[$this->loggedIn] = true;
			
			$info = array();
			$sqlQuery = "SELECT Username, Password FROM User WHERE Username LIKE ?";
			$stmt = $this->m_db->Prepare($sqlQuery);
			$stmt->bind_param("s", $username);
			
			$info = $this->m_db->ExecuteSelectUserAndPass($stmt);
			if ($info[0] == NULL)
			{
				$_SESSION[$this->loggedIn] = false;
				return false;
			}
			
			if ($info[0] == $username && $info[1] == $password)
				return true;
			
			$_SESSION[$this->loggedIn] = false;
			return false;
		}
		
		/* Logs out a user */
		public function DoLogout()
		{
			$_SESSION[$this->loggedIn] = false;
		}
		
		/* Automatic unit test on the LoginHandler's funcationality */
		public static function Test()
		{
			// The system under test
			$sut = new LoginHandler();
			 
			// Variable to indicate if any errors occurred
			$hasErrors = false;
			
			// Start in logged out mode
			$sut->DoLogout();
			
			// Check if "IsLoggedIn()" works correctly when no user is logged in
			if ($sut->IsLoggedIn())
			{
				echo "<div>Error! Function IsLoggedIn acts wrong when no user is logged in.</div>";
				$hasErrors = true;
			}
			
			// Check if "DoLogin()" works correctly with bad login information 
			if ($sut->DoLogin("fail", "error"))
			{
				echo "<div>Error! Function DoLogin acts wrong when bad information is entered.</div>";
				$hasErrors = true;
			}
			// Check if "DoLogin()" works correctly with good login information
			else if (!$sut->DoLogin(self::$users[0], self::$passwords[0]))
			{
				echo "<div>Error! Function DoLogin acts wrong when correct information is entered.</div>";
				$hasErrors = true;
			}
			
			// If everything up till now worked fine, execute additional tests
			if (!$hasErrors)
			{
				// Check if the "IsLoggedIn()" works correctly when a user is logged in
				if (!$sut->IsLoggedIn())
				{
					echo "<div>Error! Function IsLoggedIn acts wrong when a user is logged in.</div>";
					$hasErrors = true;
				}
				
				// Logout the user
				$sut->DoLogout();
				
				// Check again if "IsLoggedIn()" works correctly when no user is logged in
				if ($sut->IsLoggedIn())
				{
					echo "<div>Error! Function IsLoggedInacts wrong when a user is not logged in.</div>";
					$hasErrors = true;
				}
				
				// Check if "DoLogin()" works correctly when a user exist, but the wrong password was entered
				if ($sut->DoLogin(self::$users[0], self::$passwords[1]))
				{
					echo "<div>Error! Function DoLogin acts wrong when a wrong password is entered.</div>";
					$hasErrors = true;
				}
			}
			
			// Return a value to indicate the result (true if hasErrors, false if not)
			return !$hasErrors;
		}
	}
	
?>